import {createRouter,createWebHistory,createWebHashHistory} from 'vue-router'
import Home from '@/views/Home.vue'
import About from '@/views/About.vue'
import Login from '@/views/Login.vue'
import Article from '@/views/Article.vue'
import User from '@/views/User.vue'
import {loginCheck} from '@/checkLogin'

const router = createRouter({
    history: createWebHistory(),
    routes:[
        {path:'/home',component: Home,name: 'home'},
        // 路由守卫beforeEnter
        {path:'/about',component: About,name: 'about',meta:{needCheck:false},beforeEnter:[(to,from) => {console.log('beforeEnter')},loginCheck]},
        {path:'/article',component: Article,name: 'article'},
        {path:'/user',component: User,name: 'user'},
        {path:'/login',component: Login,name: 'login'},
    ]
});

// beforeEach(全局前置守卫)中是异步的
router.beforeEach((to,from) => {
    console.log('beforeEach');
    // return false;

    // 跳转到指定页面
    // if(to.name == 'about')
    // return '/login';

    // 跳转回去
    // if(to.name == 'about')
    // return from;
});

// 如果参数中有next，必须调用一次
// router.beforeEach((to,from,next) => {
//     if(to.name == 'about') next('/login')
//     else next();
// });


// beforeResolve(全局解析守卫)
// 如果beforeEach返回的是false,beforeResolve就不会执行
router.beforeResolve((to,from) => {
    console.log('beforeResolve');
});

// afterEach(全局后置守卫)
// 无论前面成功还是失败都会执行
router.afterEach((to,from,fail) => {
    console.log('afterEach');
    if(!fail){
        console.log('路由解析成功！')
    }
});
export default router;